<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 分销商中心及分销功能
 */
class Controller_Pc_Api_V2_Distributor extends Controller_Pc_Api_v2_Base
{
    private $result;

    /**
     * 验证类
     * @var Request_Api_V2_Distributor
     */
    private $validation;

    /**
     * 服务类
     * @var Service_Api_Fenxiao
     */
    private $service;


    public function before()
    {
        parent::before();
        $this->result = array(
            'status' => true,//默认状态为成功
            'msg' => '操作成功',//默认的提示消息
            'data' => array()
        );
        $this->validation = Request_Api_V2_Factory::create('distributor', $this->request_params);
        $this->service = new Service_Api_Fenxiao();


    }
    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /*
     * 获取当前会员的分销商信息
     */
    public function action_get_user()
    {
        try{
            $this->validation->get_user()->check();
            $info = $this->service->info($this->request_params['mid'],$this->member_info);
            $this->result['data'] = $info;

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    // 检查用户是否是分销商,如果传了产品则同时检查产品是否是分销状态.
    public function action_get_isfenxiao()
    {
        try{
            $this->validation->get_isfenxiao()->check();
            $this->result['data'] = $this->service->is_distributor($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    //我的客户列表
    public function action_get_sub_member()
    {
        try{
            $this->validation->get_sub_member()->check();
            $this->result['data'] = $this->service->get_customer($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 获取分销等级
     */
    public function action_get_level()
    {
        try{
            $this->validation->get_level()->check();
            $this->result['data'] = $this->service->get_level();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    //获得我的分销商列表
    public function action_get_subfx()
    {
        try{
            $this->validation->get_subfx()->check();
            $this->result['data'] = $this->service->get_subfx($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    // 申请加入分销商
    public function action_add_us()
    {
        try{
            $this->validation->add_us()->check();
            $this->result['data'] = $this->service->add_us($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    //推广产品
    public function action_get_product()
    {
        try{
            $this->validation->get_product()->check();
            $list = $this->service->get_product($this->request_params);
            $data = array(
                'list' => $list
            );
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    //佣金明细
    public function action_get_order()
    {
        try{
            $this->validation->get_order()->check();
            $this->result['data'] = $this->service->get_order($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取产品分销海报
    public function action_get_distribution_poster()
    {

        try{
            $this->validation->get_distribution_poster()->check();
            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->get_distribution_poster($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    //获得佣金明细查询条件
    public function action_get_order_condition()
    {
        try{
            $this->validation->get_order_condition()->check();
            $this->result['data'] = $this->service->get_order_condition();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    //获得推广商品查询条件
    public function action_get_product_condition()
    {
        try{
            $this->validation->get_product_condition()->check();
            $this->result['data'] = $this->service->get_product_condition();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }


    // 获取分销商邀请海报
    public function action_get_invite_image(){

        try{
            $this->validation->get_invite_image()->check();
            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->get_invite_image($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }
    //获得当前商品分销可赚取金额
    public function action_get_commission()
    {
        try{
            $this->validation->get_commission()->check();
            $type_id = $this->request_params['type_id'];
            $mid = $this->request_params['mid'];
            $product_id = $this->request_params['product_id'];
            $product_price = $this->request_params['price'] ?? 0;
            if (!$product_price) {
                $product_price = $this->service->get_price($type_id,$product_id);//最低价格
            }
            $this->result['data']['profit'] = floatval($this->service->get_estimate_profit($type_id,$mid,$product_id,$product_price));
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


	
	// 获取分销商相关配置说明
	public function action_get_fenxiao_config()
    {

        try{
            $this->validation->get_fenxiao_config()->check();
            $fields = $this->request_params['fields'];
            $this->result['data'] = $this->service->get_fenxiao_config($fields);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

	}


    //复制链接
    public function action_get_link()
    {
        try{
            $this->validation->get_link()->check();
            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->get_link($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 获取分销商店铺码图片
    public function action_get_shop_code()
    {
        try{
            $this->validation->get_shop_code()->check();
            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->get_shop_code($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

}